package com.jianguozh.difs.support.api.ops;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jianguozh.difs.common.bean.R;
import com.jianguozh.difs.framework.base.BaseController;
import com.jianguozh.difs.framework.constant.enums.LogTypeEnum;
import com.jianguozh.difs.support.service.base.entity.Ulog;
import com.jianguozh.difs.support.service.base.service.IUlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.*;

/**
 * <p>
 * 日志 前端控制器
 * </p>
 *
 * @author hanrs
 * @since 2022-06-10
 */
@RestController
@RequestMapping("/ulog")
public class UlogController extends BaseController {

    @Autowired
    IUlogService ulogService;

    /**
     * 由id获取对象
     */
    @GetMapping("/{id}")
    public R<Ulog> get(@PathVariable String id) {
        return success(ulogService.getById(id));
    }


    /**
     * 条件分页查询
     */
    @GetMapping("/page-query")
    public R<IPage<Ulog>> pageQuery(HttpServletRequest request,
                                    @RequestParam(name = "startDate", required = false) LocalDate startDate,
                                    @RequestParam(name = "endDate", required = false) LocalDate endDate,
                                    @RequestParam(name = "type", required = false) LogTypeEnum type,
                                    @RequestParam(name = "title", required = false) String title,
                                    @RequestParam(name = "operator", required = false) String operator

    ) {

        LambdaQueryWrapper<Ulog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(startDate)) {
            LocalDateTime start = startDate.atTime(LocalTime.MIN);
            lambdaQueryWrapper.ge(Ulog::getOperateDate, start);
        }

        if (ObjectUtil.isNotNull(endDate)) {
            LocalDateTime end = endDate.atTime(LocalTime.MAX);
            lambdaQueryWrapper.le(Ulog::getOperateDate, end);
        }

        if (null != type) {
            lambdaQueryWrapper.eq(Ulog::getType, type);
        }

        if (StrUtil.isNotBlank(operator)) {
            lambdaQueryWrapper.like(Ulog::getOperator, operator);
        }

        if (StrUtil.isNotBlank(title)) {
            lambdaQueryWrapper.like(Ulog::getTitle, title);
        }

        IPage page = resolvePage(request);
        return success(ulogService.page(page, lambdaQueryWrapper));
    }

    @DeleteMapping("/filter")
    public R<Boolean> delete(@RequestParam(name = "startDate", required = false) LocalDate startDate,
                             @RequestParam(name = "endDate", required = false) LocalDate endDate,
                             @RequestParam(name = "type", required = false) LogTypeEnum type,
                             @RequestParam(name = "title", required = false) String title,
                             @RequestParam(name = "operator", required = false) String operator) {

        LambdaQueryWrapper<Ulog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(startDate)) {
            LocalDateTime start = startDate.atTime(LocalTime.MIN);
            lambdaQueryWrapper.ge(Ulog::getOperateDate, start);
        }

        if (ObjectUtil.isNotNull(endDate)) {
            LocalDateTime end = endDate.atTime(LocalTime.MAX);
            lambdaQueryWrapper.le(Ulog::getOperateDate, end);
        }

        if (null != type) {
            lambdaQueryWrapper.eq(Ulog::getType, type);
        }

        if (StrUtil.isNotBlank(operator)) {
            lambdaQueryWrapper.like(Ulog::getOperator, operator);
        }

        if (StrUtil.isNotBlank(title)) {
            lambdaQueryWrapper.like(Ulog::getTitle, title);
        }

        ulogService.remove(lambdaQueryWrapper); //执行的国歌不做为输出结果，原因：当删除影响的行数为0时，mybatispls 返回false
        return success(true);
    }

    @DeleteMapping("/all")
    public R<Boolean> deleteAll() {
        ulogService.remove(new QueryWrapper<>());
        return success(true);
    }


}
